package com.jrx.anytxn.param.service.product.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.product.PrBlockCodeReq;
import com.jrx.anytxn.param.dto.product.PrBlockCodeRes;
import com.jrx.anytxn.param.entity.PrBlockCode;
import com.jrx.anytxn.param.mapper.base.PrBlockCodeMapper;
import com.jrx.anytxn.param.mapper.ext.ExtPrBlockCodeMapper;
import com.jrx.anytxn.param.service.product.IPrBlockCodeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author liuyu
 * @Description
 */
@Service
@CacheConfig(cacheNames = "BLOCK_CODE")
public class PrBlockCodeServiceImpl implements IPrBlockCodeService {

    private static final Logger logger = LoggerFactory.getLogger(PrBlockCodeServiceImpl.class);

    @Autowired
    private ISegmentService segmentService;
    @Resource
    private PrBlockCodeMapper prBlockCodeMapper;
    @Resource
    private ExtPrBlockCodeMapper extPrBlockCodeMapper;


    @Override
    public PrBlockCodeRes findByTenIdAndBlockCode(String tenantId, String blockCode) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(blockCode)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrBlockCode prBlockCode = extPrBlockCodeMapper.selectByTenantIdAndBlockCode(tenantId, blockCode);
        if (null == prBlockCode) {
            logger.error("封锁码数据不存在");
            throw new TxnBizException(TxnRespCode.NO_DATA.getCode(), TxnRespCode.NO_DATA.getMsg(ErrorMsgConstant.BLOCKCODE));
        }

        return BeanMapping.copy(prBlockCode, PrBlockCodeRes.class);
    }

    @Override
    public TxnPage<PrBlockCodeRes> findAllPageByTenantId(Integer pageNum, Integer pageSize, String tenantId) throws TxnException {
        Page<PrBlockCode> page = PageHelper.startPage(pageNum, pageSize, Constant.ORDER_BY_SORT);
        List<PrBlockCode> prBlockCodes = extPrBlockCodeMapper.selectByTenantId(tenantId);
        if (prBlockCodes.isEmpty()) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        List<PrBlockCodeRes> res = BeanMapping.copyList(prBlockCodes, PrBlockCodeRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public PrBlockCodeRes addPrBlockCode(PrBlockCodeReq prBlockCodeReq) throws TxnException {
        PrBlockCode code = extPrBlockCodeMapper.selectByTenantIdAndBlockCode(prBlockCodeReq.getTenantId(), prBlockCodeReq.getBlockCode());
        if (null != code) {
            logger.error("此封锁码类型已存在");
            throw new TxnException(TxnRespCode.DATA_EXISTS.getCode(), TxnRespCode.DATA_EXISTS.getMsg(ErrorMsgConstant.BLOCK_CODE_ERROR));
        }
        PrBlockCode prBlockCode = BeanMapping.copy(prBlockCodeReq, PrBlockCode.class);

        try {
            prBlockCode.setId(segmentService.getId(Constant.PR_BLOCK_CODE));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(ErrorMsgConstant.ERROR_ID);
        }
        prBlockCode.setRecordVersionNumber(Constant.VERSION_NUMBER);
        prBlockCodeMapper.insertSelective(prBlockCode);
        return BeanMapping.copy(prBlockCode, PrBlockCodeRes.class);
    }

    @CacheEvict(key = "'blockCode_'+#prBlockCodeReq.tenantId+'_'+#prBlockCodeReq.blockCode")
    @Override
    public PrBlockCodeRes modifyPrBlockCode(PrBlockCodeReq prBlockCodeReq) throws TxnException {

        PrBlockCode code = extPrBlockCodeMapper.selectByTenantIdAndBlockCode(prBlockCodeReq.getTenantId(), prBlockCodeReq.getBlockCode());
        if (null == code) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        PrBlockCode prBlockCode = BeanMapping.copy(prBlockCodeReq, PrBlockCode.class);
        extPrBlockCodeMapper.updateByTenantIdAndBlockCode(prBlockCode);

        return BeanMapping.copy(prBlockCode, PrBlockCodeRes.class);
    }

    @CacheEvict(key = "'blockCode_'+#tenantId+'_'+#blockCode")
    @Override
    public Boolean removeByTenIdAndBlockCode(String tenantId, String organizationId, String blockCode) throws TxnException {
        boolean flag = true;
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(organizationId)|| StringUtils.isBlank(blockCode)) {
            logger.error("封锁码主键ID不存在");
            throw new TxnException(TxnRespCode.NOT_EMPTY.getCode(), TxnRespCode.NOT_EMPTY.getMsg(ErrorMsgConstant.AMORT_METHOD_ID));
        }
        int a = extPrBlockCodeMapper.deleteByTenIdAndBlockCode(tenantId, organizationId, blockCode);
        if (a < 0) {
            flag = false;
        }
        return flag;
    }

    @Cacheable(key = "'blockCode_'+#tenantId+'_'+#blockCode")
    @Override
    public PrBlockCode findByBlockCode(String tenantId, String blockCode) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(blockCode)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrBlockCode prBlockCode = extPrBlockCodeMapper.selectByTenantIdAndBlockCode(tenantId, blockCode);
        if (null == prBlockCode) {
            logger.error("封锁码数据不存在");
            throw new TxnBizException(TxnRespCode.NO_DATA.getCode(), TxnRespCode.NO_DATA.getMsg(ErrorMsgConstant.BLOCKCODE));
        }

        if (Constant.ORG_STATUS_0.equals(prBlockCode.getCodeStatus())) {
            logger.error("封锁码已失效");
            throw new TxnBizException(TxnRespCode.NO_DATA.getCode(), TxnRespCode.NO_DATA.getMsg(ErrorMsgConstant.BLOCKCODE));
        }

        return prBlockCode;
    }

    @Override
    public List<PrBlockCodeRes> findAllBlockCodeByTenId(String tenantId) {
        List<PrBlockCode> prBlockCodeTables = extPrBlockCodeMapper.findAllBlockCodeByTenId(tenantId);
        return BeanMapping.copyList(prBlockCodeTables, PrBlockCodeRes.class);

    }
}
